package com.ruike.alisurface.ui.adv;

import android.media.MediaPlayer;
import android.view.View;
import android.widget.TextView;
import android.widget.VideoView;

import com.ruike.alisurface.Constant;
import com.ruike.alisurface.R;
import com.ruike.alisurface.base.BaseFgt;
import com.ruike.alisurface.bean.AdvBean;
import com.ruike.alisurface.utils.FinalDbUtils;
import com.voodoo.lib_logcat.L;
import com.voodoo.lib_utils.FileUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author：voodoo
 * CreateTime：2021/06/17 017 上午 10:32
 * Description：视频播放Fragment
 * 视频测试地址（m3u8/mp4） https://www.jianshu.com/p/cab2cd7b3f1c
 */
public class VideoAdvFragment extends BaseFgt {

    @BindView(R.id.adv_noAdv_tv)
    TextView noAdvTv;
    @BindView(R.id.adv_video_txcvv)
    VideoView videoPlayerTxcvv;
    @BindView(R.id.adv_playTime_tv)
    TextView playTimeTv;

    private int currentPlayIndex;
    private List<AdvBean> playList;


    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_adv_show;
    }

    @Override
    protected void initViews() {


    }

    @Override
    protected void initData() {
        setVideoData();
    }

    public void setVideoData() {
        // 检查文件路径是否存在，不存在的话直接创建路径
        FileUtils.checkFolderIsExists(Constant.FilePath.ADV_VIDEO_FOLDER_PATH, true);
        playList = new ArrayList<>();
        // 查询数据库，如果库中没有视频广告，那么查询文件夹，如果文件夹中有广告，将其转成对象并存到数据库
        List<AdvBean> advBeanList = FinalDbUtils.getInstance().findAllByWhere(AdvBean.class, " isImage= 1");
        L.i("查询到的数据库中视频路径：" + advBeanList.toString());
        // 数据库中有数据
        if (advBeanList != null && advBeanList.size() > 0) {
            playList.clear();
            for (AdvBean advBean : advBeanList) {
                if (new File(advBean.getFilePath()).exists()) {
                    playList.add(advBean);
                } else {
                    FinalDbUtils.getInstance().delete(advBean);
                }
            }
            advBeanList.clear();
        }
        if (playList.size() <= 0) { // 数据库中没有数据，或者查询到的路径对应的文件都不存在
            // 获取视频广告文件夹中的文件列表
            File[] folderFileList = FileUtils.getFolderFileList(Constant.FilePath.ADV_VIDEO_FOLDER_PATH);
            if (folderFileList != null && folderFileList.length > 0) {
                playList.clear();
                for (File videoFile : folderFileList) {
                    AdvBean advBean = new AdvBean(videoFile.getPath(), videoFile.getName(), 1);
                    playList.add(advBean);
                    FinalDbUtils.getInstance().save(advBean);
                }
            } else {
                // 提示无视频广告
                noAdvTv.setVisibility(View.VISIBLE);
                return;
            }
        }
        // 显示视频播放控件
        videoPlayerTxcvv.setVisibility(View.VISIBLE);
        videoPlayerTxcvv.setVideoPath(playList.get(videoIndex).getFilePath());
        videoPlayerTxcvv.start();
        videoPlayerTxcvv.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                ++videoIndex;
                if (videoIndex < playList.size()) {
                    videoPlayerTxcvv.setVideoPath(playList.get(videoIndex).getFilePath());
                    videoPlayerTxcvv.start();
                } else {
                    videoIndex = 0;
                    videoPlayerTxcvv.setVideoPath(playList.get(videoIndex).getFilePath());
                    videoPlayerTxcvv.start();
                }
            }
        });
    }

    int videoIndex = 0;

    @OnClick({R.id.adv_startPay_tv})
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.adv_startPay_tv:
                ((Adv_Aty) getActivity()).jumpToProductListActivity();
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    /**
     * 将传入的秒数转换成时长
     *
     * @param times 要转换的时长，单位秒
     * @return HH:mm:ss格式的时长
     */
    public static String conversion(int times) {
        int remainderH = times % 3600;
        String hStr = "00";
        if (times > 3600) {
            int h = times / 3600;
            hStr = h < 10 ? "0" + h : String.valueOf(h);
        }
        String mStr = "00";
        if (times > 60) {
            int m = remainderH / 60;
            mStr = m < 10 ? "0" + m : String.valueOf(m);
        }
        String sStr = "00";
        if (times > 60) {
            int s = remainderH % 60;
            sStr = s < 10 ? "0" + s : String.valueOf(s);
        } else {
            int s = times;
            sStr = s < 10 ? "0" + s : String.valueOf(s);
        }
        return (hStr.equals("00") ? "" : (hStr + ":")) + mStr + ":" + sStr;
    }


}
